package main

import (
	pb "add/proto"
	"context"
	"fmt"
	"net"
	"os"

	"google.golang.org/grpc"
)

const (
	port     = ":51051"
	unixPath = "/tmp/go-add.sock"
)

type server struct {
	pb.UnimplementedAddServer
}

func (s *server) Add(ctx context.Context, in *pb.AddRequest) (*pb.AddReply, error) {
	fmt.Printf("Received: %d + %d\n", in.A, in.B)
	return &pb.AddReply{
		Message: in.A + in.B,
	}, nil
}

func main() {
	_ = os.Remove(unixPath)
	// lis, err := net.Listen("tcp", port)
	lis, err := net.Listen("unix", unixPath)
	if err != nil {
		panic(err)
	}
	s := grpc.NewServer()
	pb.RegisterAddServer(s, &server{})
	err = s.Serve(lis)
	if err != nil {
		panic(err)
	}
}
